package com.androidcentralmedias;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SendSelect extends ListActivity {

	static ArrayList<String> enderecos = new ArrayList<String>();  
	static ArrayList<String> ip = new ArrayList<String>();  
	static ArrayList<String> canal = new ArrayList<String>();  
/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		enderecos.clear();
		ip.clear();
		canal.clear();
		try {								
            // open the file for reading
            InputStream instream = openFileInput("enderecos.txt");
            // if file the available for reading
              // prepare the file for reading
              InputStreamReader inputreader = new InputStreamReader(instream);
              BufferedReader buffreader = new BufferedReader(inputreader);
              while(buffreader.ready()){
            	  enderecos.add(buffreader.readLine());
            	  ip.add(buffreader.readLine());
            	  canal.add(buffreader.readLine());
              }
              Log.d("UDP",ip.get(0));
              Log.d("UDP",canal.get(0));
			  instream.close();
          } catch (IOException e) {
			Log.d("UDP",e.toString());
		}
		enderecos.add("Novo item");
		this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, enderecos)); //essa funcao cria apartir da lista filmes usando um layout do proprio android uma lista com os nomes
		 ListView listView = getListView();
		 listView.setTextFilterEnabled(true); 
	}
	protected void onListItemClick(ListView l, View v, int position, long id) {  //aqui vc coloca a funcao atribuida a cada nome quando eles sao clicados
		super.onListItemClick(l, v, position, id);
		// Get the item that was clicked								//Aqui vc pega o nome do item q vc clico
		if (enderecos.get(position).equals("Novo item")){
			startActivity(new Intent(this, AddEndereco.class));
		}
		else{
			if (List.lista!=null)
				Client.enviar("?" +MainActivity.tipo + ":" + List.keyword + ":" + canal.get(position)+ ":" + List.lista, MainActivity.type,1);								//Aqui envia o nome do filme como acao
			else
				Client.enviar("?" +MainActivity.tipo + ":" + List.keyword + ":" + canal.get(position) + ":", MainActivity.type,1);	
		Log.d("UDP",ip.get(position));
		if (!ip.get(position).equals("")){
			Client.ip2 = ip.get(position);
			Client.enviar("?play:" + Client.ip1 + ":" + canal.get(position), MainActivity.type, 2);
		}
		startActivity(new Intent(this, Play.class));
		Play.canal = canal.get(position);
		Play.endereco = enderecos.get(position);
		finish();
		}
	}
}